package com.ebelter.ty.ui.fragment.scale.others;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.ty.R;
import com.ebelter.ty.model.http.TyNetUtil;
import com.ebelter.ty.model.http.bean.OriginalParametersResponse;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B5_Fragment;
import com.ebelter.ty.ui.views.BirthdayView;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


import static com.ebelter.ty.ui.fragment.scale.others.Scale_M_NAME_Fragment.updateLocalData;


public class Scale_M_BIRTHDAY_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "Scale_M_NAME_Fragment";


    private ImageView modifybirthday_back_bt;
    private BirthdayView bdv_birthday_pager;
    private TextView modifybirthday_done_tv;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_modifybirthday;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);

        bdv_birthday_pager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                bdv_birthday_pager.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                int year = UserSpUtil.readInt(Constants.IUser.YEAR);
                int month = UserSpUtil.readInt(Constants.IUser.MONTHS);
                int day = UserSpUtil.readInt(Constants.IUser.DAY);

                bdv_birthday_pager.setCurrentDate(year,month,day);
            }
        });

    }

    private void FV() {
        modifybirthday_back_bt = mRootView.findViewById(R.id.modifybirthday_back_bt);

        modifybirthday_done_tv = mRootView.findViewById(R.id.modifybirthday_done_tv);
        bdv_birthday_pager = mRootView.findViewById(R.id.bdv_birthday_pager);

        modifybirthday_back_bt.setOnClickListener(this);
        modifybirthday_done_tv.setOnClickListener(this);



    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.modifybirthday_back_bt:
                getActivity().finish();
                break;
            case R.id.modifybirthday_done_tv:
                modifyBirthday();
                break;

        }
    }

    private void modifyBirthday() {

        String birthday=bdv_birthday_pager.getBirthday().getFatterDay();
        LogUtils.i(TAG,"---"+birthday);
        String originalBirthday = UserSpUtil.readString(Constants.IUser.BIRTHDAY);
        if (TextUtils.equals(birthday,originalBirthday)){
            //说明相同就直接提示修改成功就好了
            ToastUtil.show(R.string.Modify_Success);
            getActivity().finish();
        }else {


            if (!NetUtils.available()){
                ToastUtil.show(R.string.Network_error);
                return;
            }

            Map<String,Serializable> paramMap=new HashMap<>();
            paramMap.put("userId", UserSpUtil.readLong(Constants.IUser.USER_ID));
            paramMap.put("birthday", birthday);

            TyNetUtil.getInstance().updateInfo(this, paramMap, new HttpResponse<OriginalParametersResponse>() {
                @Override
                public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse,String s) {
                    if (isRequestSuccess){

                        if (originalParametersResponse.resultCode==0){
                            ToastUtil.show(R.string.Modify_Success);
                            updateLocalData((HashMap<String, Serializable>) originalParametersResponse.originalParameters);

                            CommonEventBus eventBus=new CommonEventBus(TAG, Scale_M_B5_Fragment.TAG,2,null);
                            EventBus.getDefault().post(eventBus);

                            if (getActivity()!=null)getActivity().finish();
                        }else {
                            ToastUtil.show(R.string.Modify_Failed);
                        }

                    }else {
                        ToastUtil.show(R.string.Network_connection_error);
                    }



                }
            });


        }

    }
}
